package uk.ac.aber.paws.client.config;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 16th August 2011
 * 
 * ConfigManager.java
 * 
 * Contains all generic constants used within the application
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;

import uk.ac.aber.paws.client.constants.Constants;
import uk.ac.aber.paws.client.core.Initiator;

/**
 * Loads and stores the configuration data for the application.
 * @author Rob Johnson
 *
 */
public class ConfigManager {

	private Properties mergedProperties = null;
	
	public ConfigManager(String configPath, String osConfig, boolean inJar) {
		
		mergedProperties = loadProperties(configPath,inJar);
		
		// add the os specific properties
		if (osConfig != null) {
			mergedProperties.putAll(loadProperties(osConfig,inJar));
		}
		
		System.out.println(mergedProperties.toString());
	}

	private Properties loadProperties(String configPath, boolean inJar) {

		Properties properties = new Properties();
		  
		try {
	        
	        if (configPath == null) {
	    		URL url = Thread.currentThread().getContextClassLoader().getResource("");
	 
	                if (url != null) {
						properties.load(url.openStream());
	                }
	            } 
	            else {
	                
	            	InputStream is = null;
	            	
	            	if (inJar) {
		            	// loads the config.properties file
	            	is = ConfigManager.class.getClassLoader().getResourceAsStream(configPath);
	            	
	            	
	        	}
	        	else {
	        		is = new FileInputStream(configPath);
	            	
	        	}
	        	
	        	if (is != null) {
	        		properties.load(is);
	        	}
	        	else {
	        		Initiator.LOG_FILE.log(Level.SEVERE,"Failed to load application config file");
	        	}
	        }
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			Initiator.LOG_FILE.log(Level.ALL,e.toString());
		}
		
		return properties;
    }
	
	/**
	 * Returns the value for the given property name
	 * @param name The name of the config property
	 * @return The value for the config property
	 * @throws IOException 
	 */
	public String getProperty(String name) {

        if (mergedProperties == null) {
           
			loadProperties(null,true);
        }	
 
        return (mergedProperties.getProperty(name)==null) ? Constants.UNDEFINED : mergedProperties.getProperty(name).trim();
    }
	
	/**
	 * Sets the given configuration property
	 * @param property The config property
	 * @param value The value to be set for this property
	 */
	public void setProperty(String property, String value) {
		
		mergedProperties.setProperty(property, value);
	}
	
	public Properties getAllProperties() {
		return mergedProperties;
	}
}

